﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Manage.Controller;


namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// PrizeRecordAjax 的摘要说明
    /// </summary>
    public class PrizeRecordAjax : IHttpHandler
    {
        PrizeRecordController Controller = new PrizeRecordController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "BatchEdit":
                        BatchEdit(context);
                        break;
                    case "GetPrizeState":
                        GetPrizeState(context);
                        break;
                    case "GetList":
                        GetList(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Select":
                        Select(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void BatchEdit(HttpContext context)
        {
            string startTime = context.Request["startTime"];
            string endTime = context.Request["endTime"];
            string state1 = context.Request["state1"];
            string state2 = context.Request["state2"];
            string userAccount = context.Request["userAccount"];
            bool isSuccess = Controller.BatchEdit(startTime, endTime, state1, state2, userAccount);
            context.Response.Write(isSuccess);
        }
        private void GetPrizeState(HttpContext context)
        {
            List<PrizeStateEnum> list = new List<PrizeStateEnum>();
            foreach (var item in Enum.GetValues(typeof(EnumUserPrizeStuas)))
            {
                PrizeStateEnum model = new PrizeStateEnum()
                {
                    NAME = item.ToString(),
                    ID = Convert.ToInt32(item)
                };
                list.Add(model);
            }
            context.Response.Write(JsonHelper.ListToJson<PrizeStateEnum>(list));
        }
        private void GetList(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);
            int count = 0;
            List<PrizeRecord> list = Controller.GetList(pageIndex, pageSize, out count);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].UserName = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].UserName);
                list[i].UserAddr = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].UserAddr);
            }
            string json = JsonHelper.ListToJson<PrizeRecord>(list, count.ToString());
            context.Response.Write(json);
        }
        private void Update(HttpContext context)
        {
            int Id = Convert.ToInt32(context.Request["id"]);
            int state = Convert.ToInt32(context.Request["state"]);
            PrizeRecord model = new PrizeRecord()
            {
                ID = Id,
                State = state,
                EditTime = Convert.ToDateTime(DateTime.Now)
            };
            context.Response.Write(Controller.UpdateState(model));
        }
        private void Select(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);
            int count = 0;
            string state = context.Request["state"];
            int SelectByID = Convert.ToInt32(context.Request["selectByID"]);
            string StartTime = null;
            string EndTime = null;
            if (context.Request["StartTime"] != null)
            {
                StartTime = context.Request["StartTime"];
                EndTime = context.Request["EndTime"];
            }
            List<PrizeRecordShow> list = Controller.GetListBySelect(pageIndex, pageSize, out count, SelectByID, StartTime, EndTime, state);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].UserName = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].UserName);
                list[i].UserAddr = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].UserAddr);
            }
            string json = JsonHelper.ListToJson<PrizeRecordShow>(list, count.ToString());
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}